package com.alphathink.deliveryassist;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.AlphaThink.DeliveryAssist.R;
import com.alphathink.deliveryassist.eneity.Contact;
import com.alphathink.deliveryassist.eneity.ContactBean;
import com.alphathink.deliveryassist.util.DisplayUtil;
import com.alphathink.deliveryassist.util.KeyUtil;
import com.alphathink.deliveryassist.util.ScreenAdapter;
import com.alphathink.deliveryassist.util.SharePrefUtil;
import com.google.gson.Gson;

/**
 * 这是要选择模式的界面
 * 
 * @author xpg
 * 
 */
public class ModelSelectActivity extends Activity {
	/** 用作标识 */
	private static final String TAG = "MainAcriviry";
	/** 主界面中的图片，logo，电话模式，短信模式 */
	private ImageView mLogoIV, mPhoneModelIV, mMessageModelIV;
	/** Button 电话模式，短信模式 */
	private Button mPhoneModelBTN, mMessageModelBTN;
	/** 提示的tip */
	private TextView mTipTV;

	/** 注册一个监听器 */
	private WigetListener listener = null;

	/** 测试的时候使用的一个实体类 */
	private ContactBean bean = null;
	private Contact contact = null;

	private Gson gson = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_model_select);

		// testApp();
		initView();
	}

	/**
	 * 用作測試加密之后的数据
	 */
	private void testApp() {
		bean = new ContactBean();
		bean.setContactNum("15517522630");
		bean.setCintactTip("这是测试的tip");
		bean.setContactAddress("测试5");
		bean.setContactId(1);
		bean.setContactName("小辉");
		gson = new Gson();

		List<ContactBean> beans = new ArrayList<ContactBean>();
		beans.add(bean);
		contact = new Contact(beans, "Test");
		String str = gson.toJson(contact);
		System.out.println("str=======" + str);
		String tmp = KeyUtil.encrypt(str);
		System.out.println("tmp===" + tmp);
		Log.e(TAG, tmp);
	}

	private void initView() {
		ScreenAdapter adapter = new ScreenAdapter(ModelSelectActivity.this);
		Typeface typeface = Typeface.createFromAsset(getAssets(),
				"font/FangZhengGB18030.ttf");

		listener = new WigetListener();
		mLogoIV = (ImageView) findViewById(R.id.main_logo_IV);

		Log.e(TAG, String.valueOf(DisplayUtil.px2dip(ModelSelectActivity.this,
				adapter.getmWindthPx())));

		mMessageModelIV = (ImageView) findViewById(R.id.main_messageModel_IV);

		mMessageModelBTN = (Button) findViewById(R.id.BTN_messageModel_main);
		String mPhoneStr = getResources().getString(R.string.messageModel);
		Spannable spannable = new SpannableString(mPhoneStr);
		spannable.setSpan(typeface, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		mMessageModelBTN.setText(spannable);
		mPhoneModelBTN = (Button) findViewById(R.id.BTN_phoneModel_main);
		mPhoneModelIV = (ImageView) findViewById(R.id.main_phoneModel_IV);
		mTipTV = (TextView) findViewById(R.id.TV_tip_main);
		mTipTV.setTypeface(typeface);
		mPhoneModelBTN.setOnClickListener(listener);
		mMessageModelBTN.setOnClickListener(listener);
	}

	class WigetListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			SharedPreferences preferences = SharePrefUtil
					.getPref(getApplicationContext());
			SharedPreferences.Editor editor = preferences.edit();

			Intent intent = new Intent();
			switch (v.getId()) {
			case R.id.BTN_phoneModel_main:
				editor.putString(SharePrefUtil.SELECTMODEL, "0");
				editor.commit();
				Log.d(TAG, "进入到电话模式");
				intent.setClass(ModelSelectActivity.this, MainActivity.class);
				startActivity(intent);
				ModelSelectActivity.this.finish();
				break;
			case R.id.BTN_messageModel_main:
				editor.putString(SharePrefUtil.SELECTMODEL, "1");
				editor.commit();
				Log.d(TAG,
						"进入到短信模式---"
								+ preferences.getString(
										SharePrefUtil.SELECTMODEL, null));
				intent.setClass(ModelSelectActivity.this, MainActivity.class);
				startActivity(intent);
				ModelSelectActivity.this.finish();
				break;
			default:
				break;
			}

		}
	}

}
